package com.hframe.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 全局线程池管理
 *
 * @author Administrator
 */
public class ThreadPool {
    private static ThreadPool interfac = null;
    ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);

    public static ThreadPool get() {
        if (null == interfac)
            interfac = new ThreadPool();

        return interfac;
    }

    /**
     * 关闭线程池
     */
    public static void shutdown() {
        if (null != interfac)
            interfac.threadPool.shutdown();
    }

    /**
     * 立即执行任务
     *
     * @param runnable
     */
    public void execute(Runnable runnable) {
        threadPool.execute(runnable);
    }

    /**
     * 延迟执行任务
     *
     * @param runnable
     * @param delay    延迟时间
     * @param unit     时间单位
     */
    public void execute(Runnable runnable, long delay, TimeUnit unit) {
        threadPool.schedule(runnable, delay, unit);
    }

    /**
     * 延迟重复执行任务
     *
     * @param runnable
     * @param delay    延迟时间
     * @param period   重复执行间隔时间
     * @param unit     时间单位
     */
    public void execute(Runnable runnable, long delay, long period, TimeUnit unit) {
        threadPool.scheduleAtFixedRate(runnable, delay, period, unit);
    }

}
